package com.greendragon.web.action.account; 

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.greendragon.control.constant.GlobalConstants;
import com.greendragon.persistence.domain.User;
import com.greendragon.service.frontend.IUserFrontendService;
import com.greendragon.util.EncryptionUtil;
import com.greendragon.web.action.BaseAction;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;

/** 
 * @author wulibin, exceptsky@gmail.com, 2011-11-15 下午09:16:21
 * 
 */
public class RetakePasswordByAnswerTwoAction extends BaseAction{
	private static final long serialVersionUID = -9185704050786970475L;
	
	private static Logger          	logger           = Logger.getLogger(RetakePasswordByAnswerAction.class);
	private IUserFrontendService   	userService;
	private ImageCaptchaService    	captchaService;
	
    private String                	answer;
    private String                	password;
    private String                	confirmPassword;
	private String 					token;
	private String 					captcha;
    
    public String execute() throws Exception {
    	User user = (User) getSession().get(GlobalConstants.SESSION_FORGET_PASSWORD_USER);
        if (null == user) {
            //返回到第一步
            return ERROR;
        }else{
            if (answer.trim().equals(user.getSecurityAnswer())) {
            	// 重设密码
            	String newPwMd5 = EncryptionUtil.encodeMD5(password); 
            	user.setPassword(newPwMd5);
            	userService.update(user);
            	getSession().remove(GlobalConstants.SESSION_FORGET_PASSWORD_USER);
            	getSession().remove(GlobalConstants.SESSION_QUESTION);
                return SUCCESS;
            } else {
                // 返回到第二步
                addFieldError("answer", getText("securityAnswer.wrong"));
                return INPUT;
            }
        	
        }
    }

    @SkipValidation
    public void validate() {
    	//已经有错误，就不再校验
    	if(getFieldErrors().size()!=0){
    		return;
    	}
    	
    	if(token!=null){
    		//校验验证码
   		 Boolean isResponseCorrect = Boolean.FALSE;
            try {
                isResponseCorrect = captchaService.validateResponseForID(token,captcha.toUpperCase());
                if(!isResponseCorrect){
               	 	addFieldError("captcha", "captcha.fail");
                }
            } catch (CaptchaServiceException e) {
           	 	logger.error(e);
           	 	addFieldError("captcha", "captcha.fail");
            }
    	}else{
    		addFieldError("captcha", "captcha.fail");
    	}
    }

	public void setUserService(IUserFrontendService userService) {
		this.userService = userService;
	}

	public void setCaptchaService(ImageCaptchaService captchaService) {
		this.captchaService = captchaService;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public String getAnswer() {
		return answer;
	}

	public String getPassword() {
		return password;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public String getCaptcha() {
		return captcha;
	}
}
 